package com.story.modular.app.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.story.common.Util.PageResult;
import com.story.common.persistence.model.CategoryClass;
import com.story.common.persistence.model.CategoryEntity;
import com.story.core.base.controller.BaseController;
import com.story.modular.app.service.ICategoryclassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;

import java.util.HashMap;
import java.util.Map;

/**
 * 首页类别控制器
 *
 * @author fengshuonan
 * @Date 2020-04-29 15:42:54
 */
@Api(value = "app首页顶部分类", description = "app首页顶部分类")
@Controller
@RequestMapping("/categoryclass")
public class CategoryclassController extends BaseController {

    private String PREFIX = "/app/categoryclass/";

    @Autowired
    private ICategoryclassService categoryclassService;
    /**
     * 跳转到首页类别首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "categoryclass.html";
    }

    /**
     * 跳转到添加首页类别
     */
    @RequestMapping("/categoryclass_add")
    public String categoryclassAdd() {
        return PREFIX + "categoryclass_add.html";
    }

    /**
     * 跳转到修改首页类别
     */
    @RequestMapping("/categoryclass_update/{categoryclassId}")
    public String categoryclassUpdate(@PathVariable Integer categoryclassId, Model model) {
        CategoryClass categoryClass=categoryclassService.findById(categoryclassId);
        model.addAttribute("categoryclass",categoryClass);
        return PREFIX + "categoryclass_edit.html";
    }

    /**
     * 获取首页类别列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String name,@RequestParam(required=false,defaultValue="1") int page,
                       @RequestParam(required=false,defaultValue="10") int limit) {
        Map<String,Object> map=new HashMap<String,Object>();
        if (!"".equals(name) && name !=null){
            map.put("name",name);
        }
        Page<CategoryClass> page1 = new Page<CategoryClass>();

        //page.setRecords(categoryService.queryPageList(page,condition));

        page1.setCurrent(page);
        page1.setSize(limit);
        PageResult pageResult=new PageResult();
        pageResult.setData(categoryclassService.queryPageList(page1,name));
        pageResult.setCode("0");
        int total=page1.getTotal();
        System.out.println(total);
        pageResult.setCount(page1.getTotal());

        pageResult.setMsg("首页顶部分类");
        return pageResult;
    }
    /**
     * 获取首页分类列表 不带分页
     */
    @ApiOperation(value="获取首页顶部分类", notes="获取首页顶部分类接口")
    @RequestMapping(value = "/queryList",method = RequestMethod.POST)
    @ResponseBody
    public Object querylist(CategoryClass categoryClass) {
        return categoryclassService.quyerList(categoryClass);
    }

    /**
     * 新增首页类别
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(CategoryClass categoryClass) {
        categoryclassService.save(categoryClass);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除首页类别
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam(value = "ids") String ids) {
        String[] idArray=ids.split(",");
        for (int i=0;i<idArray.length;i++){
            categoryclassService.delete(categoryclassService.findById(Integer.parseInt(idArray[i])));
        }

        return SUCCESS_TIP;
    }


    /**
     * 修改首页类别
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(CategoryClass categoryClass) {
        if(categoryclassService.update(categoryClass)>0){
            return super.SUCCESS_TIP;
        }else {
            return super.ERROR;
        }

    }

    /**
     * 首页类别详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail(@PathVariable Integer categoryclassId, Model model) {
        CategoryClass categoryClass=categoryclassService.findById(categoryclassId);
        model.addAttribute("categoryclass",categoryClass);
        return null;
    }
}
